package com.example.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IdentityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 这里可以实现登录验证的逻辑，例如检查session中的用户信息
        if (request.getSession().getAttribute("user") == null) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value()); // 未登录则返回401错误
            return false;
        }
        return true; // 登录成功则继续执行后续的请求处理
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 可以在这里添加登录后的一些处理逻辑
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 请求处理完成后的一些处理逻辑
    }
}